route.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ResultDto } from '@/types/response/common';
  3. import { fetchJson } from '@/lib/utils/server';
  4. function buildEndpoint(path?: string[]) {
  5. const subPath = path ? `/${path.join('/')}` : '';
  6. return `/api/channel-titles${subPath}`;
  7. }
  8. export async function GET(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  9. const { path } = await params;
  10. const endpoint = buildEndpoint(path);
  11. const url = new URL(request.url);
  12. const res: ResultDto = await fetchJson(`${endpoint}${url.search}`, { method: 'GET' });
  13. return NextResponse.json(res);
  14. }
  15. export async function POST(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  16. const { path } = await params;
  17. const endpoint = buildEndpoint(path);
  18. const contentType = request.headers.get('content-type') || '';
  19. // 멀티파트 업로드는 boundary 보존을 위해 arrayBuffer로 그대로 전달
  20. if (contentType.includes('multipart/form-data')) {
  21. const res: ResultDto = await fetchJson(endpoint, {
  22. method: 'POST',
  23. body: await request.arrayBuffer(),
  24. headers: { 'Content-Type': contentType }
  25. });
  26. return NextResponse.json(res);
  27. }
  28. const res: ResultDto = await fetchJson(endpoint, {
  29. method: 'POST',
  30. body: (await request.text()) || '{}'
  31. });
  32. return NextResponse.json(res);
  33. }
  34. export async function PUT(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  35. const { path } = await params;
  36. const endpoint = buildEndpoint(path);
  37. const contentType = request.headers.get('content-type') || '';
  38. if (contentType.includes('multipart/form-data')) {
  39. const res: ResultDto = await fetchJson(endpoint, {
  40. method: 'PUT',
  41. body: await request.arrayBuffer(),
  42. headers: { 'Content-Type': contentType }
  43. });
  44. return NextResponse.json(res);
  45. }
  46. const res: ResultDto = await fetchJson(endpoint, {
  47. method: 'PUT',
  48. body: (await request.text()) || '{}'
  49. });
  50. return NextResponse.json(res);
  51. }
  52. export async function DELETE(_request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  53. const { path } = await params;
  54. const endpoint = buildEndpoint(path);
  55. const res: ResultDto = await fetchJson(endpoint, { method: 'DELETE' });
  56. return NextResponse.json(res);
  57. }